import { getUserInfo } from "."

export const BASE_URL =process.env.NODE_ENV === 'development' ? '/api' : 'https://oa.tasystem-bj.online:8901/api'

export const BASE_LOCATION_URL = process.env.NODE_ENV === 'development'? '/location' : 'https://apis.map.qq.com'

const request = ({
  url = '',
  method = 'POST',
  data = {},
  isLoading = true
}) => {
  return new Promise((resolve, reject) => {
    if (isLoading) {
      uni.showLoading({
        mask: true,
        title: '加载中...'
      })
    }
    const info = getUserInfo()
    uni.request({
      url: BASE_URL + url,
      header: {
        authorization: info.token,
        userId: info.userId
      },
      method: method,
      data,
      success: (res) => {
        if (res.data.code == 401) {
          return uni.reLaunch({
            url: '/pages/login/login'
          })
        }
        if (res.data.code === '0000') {
          if (res.data.dataCount) {
            res.data.data.dataCount = res.data.dataCount
          }
          resolve(res.data.data)
        } else {
          uni.showToast({
            title: res.data.msg || 'err',
            icon: 'none'
          })
        }
      },
      fail: (err) => {
        uni.showToast({
          title: '接口异常，请稍后再试',
          icon: 'none'
        })
        reject(null)
      },
      complete() {
        uni.hideLoading()
      }
    })
  })
}

export default request